package
{
	import flash.geom.Point;

	public class MatchSet
	{
		public var gameObjects:Vector.<GameObject> = new Vector.<GameObject>;
		public var markForDestroy:Boolean = false;
		public var m_lastItemAdded:GameObject;
		public var convergencePoint:Point;
		public function MatchSet()
		{
		}

		
		public function add(gameObject:GameObject):void{
			gameObjects.push(gameObject);
		}
		
		public function test(gameObject:GameObject):Boolean{
			var i:int, l:int;
			l = gameObjects.length;
			var isAdjacent:Boolean = false;
			for(i = 0; i < l; i++){
				var validLocations:Array = [];
				var gameObjectLocation:Point = gameObjects[i].nextLocation;
				
				switch(true){
					case (gameObject.nextLocation.subtract(new Point(gameObjectLocation.x-1, gameObjectLocation.y)).length < 1):
						isAdjacent = true;
						break;
					case (gameObject.nextLocation.subtract(new Point(gameObjectLocation.x+1, gameObjectLocation.y)).length < 1):
						isAdjacent = true;
						break;
					case (gameObject.nextLocation.subtract(new Point(gameObjectLocation.x, gameObjectLocation.y - 1)).length < 1):
						isAdjacent = true;
						break;
					case (gameObject.nextLocation.subtract(new Point(gameObjectLocation.x, gameObjectLocation.y + 1)).length < 1):
						isAdjacent = true;
						break;				
				}
			}
			var isSameType:Boolean = false;
			if(gameObject.type == gameObjects[0].type){
				isSameType = true;
			}
			
			var isSameLevel:Boolean = false;
			if(gameObject.level == gameObjects[0].level){
				isSameLevel = true;
			}
			
			return (isAdjacent && isSameType && isSameLevel);
		}
		
		public function createMatchedItem():GameObject{
			var go:GameObject = new GameObject();
			go.type = gameObjects[0].type;
			go.level = gameObjects[0].level + 1;
			go.updateDisplay();
			return go;
		}
		
	}
}